home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Libris Britannia 4
/
science library(b).zip
/
science library(b)
/
PROGRAMM
/
PASCAL
/
0921.ZIP
/
JOYSTK.ARC
/
JOYSTICK.DOC
< prev
next >
Wrap
Text File
|
1987-03-29
|
3KB
|
68 lines
TURBO PASCAL JOYSTICK ROUTINES
The IBM joystick port is located at hex 201. All the information
about both joysticks can be read in the byte at that port. Each bit reports
on a specific thing, namely:
Bit Reports on
0 Joystick A X-Axis Coordinate
1 Joystick A Y-Axis Coordinate
2 Joystick B X-Axis Coordinate
3 Joystick B Y-Axis Coordinate
4 Joystick A Button #1 (this is usually the top button)
5 Joystick A Button #2
6 Joystick B Button #1
7 Joystick B Button #2
To read the status of a button, you simply read the relevant bit. If
the bit is 1, the button is not pressed; if it is 0, the button is pressed.
Aside from the fact that this relationship seems counterintuitive, reading the
buttons is a simple task, easily accomplished with the Turbo Pascal port array
and a little bit masking.
The stick itself is a different story. How is it possible to get
enough information from one bit? The answer is that you get it by repeated
reads of the relevant bit. The process works like this: when you output
something--anything--to port 201, the lower four bits are all set to 1
automatically. (I think of this as "nudging" the port.) The length of time
it takes each bit to return to 0 depends on the position of the stick. For
example, if Joystick A is all the way to the left, Bit 0 will bounce back to 0
immediately after a nudge. If it is all the way to the right, it will take
longer to return to 0. If it is centered, it will take about half as long to
return to 0 as it does when it is all the way to the right.
You can use the Turbo Pascal port array in a loop to make successive
reads of port 201, but that's very slow. The successive reads are too far
apart to be very reliable as a direction indicator, and they are woefully
inadequate to use as a position indicator. (If you don't understand the
distinction between a direction indicator and a position indicator, compare
JOYDEMO2.PAS with JOYDEMO3.PAS.)
Only machine code is fast enough to get a good reading of the joystick
port. And, of course, Turbo Pascal makes that easy (at least as easy as it
ever can be) with the inline procedure. READJOY.INC is an inline procedure
written by me to do the job. I hope you find it useful.
The ARC file that includes this file comprises seven others. This may
be a bit of overkill, but I thought some might find it helpful to have as much
information as possible. The files have comments where I thought a bit of
exegesis might be in order. The eight files are:
JOYSTICK.DOC This file.
READJOY.INC The key file, with the inline procedure.
BUTTONS.INC Functions to read the joystick buttons.
JOYPRSNT.INC Function to determine whether a joystick is installed.
JOYDEMO1.PAS
JOYDEMO2.PAS
JOYDEMO3.PAS
JOYDEMO4.PAS
I would appreciate any comments or suggestions.
David Howorth
CIS User ID: 71600,521
March 29,1987